<?php 

include("../config.php");
include("../thumb.php");

$thumbs = array();
$query='SELECT * FROM thumb_size';
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
	$thumbs[$i] = array(
		'w'=>mysql_result($result, $i, "width"),
		'h'=>mysql_result($result, $i, "height"),
		'crop'=>mysql_result($result, $i, "crop"),
		'id'=>mysql_result($result, $i, "id"));
	$i++;
}



$query='SELECT * FROM gallery g 
	left join gallery_thumb gt on gt.gallery_id = g.id 
 	where gt.path is null';

$result=mysql_query($query);
$num=mysql_numrows($result);

$i=0;
while ($i < $num) {
	
	$imagedir=mysql_result($result,$i,"imagedir");
	$galleryid=mysql_result($result,$i,"id");
	$fullpath="../".$gconfig['image-root']."/".$imagedir;
	
	$files = scandir($fullpath);
	natsort($files);
	$file = $files[count($files)-1];
		
	$imagepath = $fullpath."/".$file;
	
	foreach ($thumbs as $thumbsetting) {
		$thumb = resize($imagepath,$thumbsetting,'../thumbs/');
		$thumb = substr($thumb, 3);
		$insert = "insert into gallery_thumb values (".$galleryid.", ".$thumbsetting['id'].", '".$thumb."')";
		mysql_query($insert) or die(mysql_error());
	}
	
	$i++;
}

mysql_close();

?>

Done